package o2o.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author: guo_wei
 * @Date: 2021-06-21
 * @Description: ip地址获取
 */
public class IPAddressUtil {
    /**
     * 获取本机IP地址
     */
    public static String getLocalHost() {
        try {
            //获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
            InetAddress address = InetAddress.getLocalHost();
            //192.168.0.121
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定URL的IP地址
     */
    public static String getUrlIpAddress(String url) {
        try {
            //获取的是该网站的ip地址，比如我们所有的请求都通过nginx的，所以这里获取到的其实是nginx服务器的IP地址
            InetAddress address = InetAddress.getByName(url);
            return address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }
}
